home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / basics / simpleeditsdi.win / qtime.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  9.8 KB  |  423 lines

  1.  /*
  2.     File:        QTime.c
  3.  
  4.     Written by: Keith Gurganus
  5.  
  6.       Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  7. */
  8.  
  9. #include "QTML.h"
  10. #include "Movies.h"
  11. #include "stdwin.h"
  12. #include "QTime.h"
  13. #include "TextUtils.h"
  14.  
  15. #define USEEXPLORERSTYLE (LOBYTE(LOWORD(GetVersion()))>=4)
  16.  
  17. static UINT APIENTRY GenericHook(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  18. void CreateNewMovieController(HWND hwnd, Movie theMovie, MovieController *theMC);
  19. Boolean MCFilter(MovieController mc, short action, void*params, long refCon);
  20. void GetMaxBounds(Rect *maxRect);
  21.  
  22. BOOL GetFile(char *fileName)
  23. {
  24.     OPENFILENAME    ofn;
  25.  
  26.     memset(&ofn, 0, sizeof(OPENFILENAME));
  27.     *fileName = '\0';
  28.     ofn.lStructSize = sizeof(OPENFILENAME);
  29.     ofn.hwndOwner = GetActiveWindow();
  30.     ofn.lpstrFile = (LPSTR)fileName;
  31.     ofn.nMaxFile  = 255;
  32.     ofn.lpstrFilter  = "QuickTime Movies (*.mov;*.avi) \0 *.mov;*.avi\0All Files (*.*) \0 *.*\0";    // Robert requested *.* to be added 07/10/96 BSF
  33.     ofn.nFilterIndex = 1;
  34.     ofn.lpstrInitialDir = NULL;
  35.     if(USEEXPLORERSTYLE)
  36.         ofn.Flags |= OFN_ENABLEHOOK | OFN_EXPLORER;
  37.     else
  38.         ofn.Flags |= OFN_ENABLEHOOK;
  39.     ofn.lpfnHook = GenericHook;
  40.  
  41.     if (GetOpenFileName(&ofn))
  42.     {
  43.         return TRUE;
  44.     } else
  45.         return FALSE;
  46. }
  47.  
  48.  
  49. static UINT APIENTRY GenericHook(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  50. {
  51.     switch (uMsg)
  52.     {
  53.         case WM_INITDIALOG:        // Center window
  54.         {
  55.             Point    ptTopLeft;
  56.             RECT    rcWindow;
  57.             BOOL    retValue;
  58.             HWND    theWnd = hWnd;
  59.             RECT    rcDesktopWindow;
  60.             long    width;
  61.             long    height;
  62.  
  63.             // if we are using 95 or NT 4.0 use the new explorer style
  64.             if(USEEXPLORERSTYLE)
  65.                 theWnd = GetParent(hWnd);
  66.  
  67.             GetWindowRect(theWnd, &rcWindow);
  68.  
  69.             width = rcWindow.right - rcWindow.left;
  70.             height = rcWindow.bottom - rcWindow.top;
  71.             GetWindowRect(GetDesktopWindow(), &rcDesktopWindow);
  72.             ptTopLeft.h = (short)((rcDesktopWindow.right + rcDesktopWindow.left)/2 - width/2);
  73.             ptTopLeft.v = (short)((rcDesktopWindow.top + rcDesktopWindow.bottom)/3 - height/3);
  74.         
  75.             retValue = SetWindowPos(theWnd, 0, ptTopLeft.h, ptTopLeft.v, 0, 0, SWP_NOZORDER|SWP_NOSIZE);
  76.     
  77.             return true;
  78.         }
  79.     }
  80.     return 0;
  81. }
  82.  
  83. BOOL OpenMovie(HWND hwnd, MovieStuff *movieStuff)
  84. {
  85.     BOOL    isMovieGood = FALSE;
  86.  
  87.     if ( strlen ((char*)movieStuff->filename ) != 0)
  88.     {
  89.         OSErr                err;
  90.         short                theFile = 0;
  91.         long                controllerFlags = 0L;
  92.         FSSpec                sfFile;
  93.         short                movieResFile;
  94.         char                theFullPath[255];
  95.  
  96.         // make a copy of our full path name
  97.         strcpy ( (char *)theFullPath, (const char *) movieStuff->filename );
  98.  
  99.         // convert theFullPath to pstring
  100.         c2pstr((char*)theFullPath);
  101.  
  102.         // Make a FSSpec with a pascal string filename
  103.         FSMakeFSSpec(0,0L,theFullPath, &sfFile);
  104.         
  105.         // Set the port    
  106.         SetGWorld((CGrafPtr)GetNativeWindowPort((void *)hwnd), nil);
  107.  
  108.         // Open the movie file
  109.         err = OpenMovieFile(&sfFile, &movieResFile, fsRdPerm);
  110.         if (err == noErr)
  111.         {
  112.             // Get the Movie from the file
  113.             err = NewMovieFromFile(&movieStuff->theMovie,movieResFile, 
  114.                                     nil, 
  115.                                     nil, 
  116.                                     newMovieActive, /* flags */
  117.                                     nil);
  118.         
  119.             // Close the movie file
  120.             CloseMovieFile(movieResFile);
  121.  
  122.             if (err == noErr)
  123.             {
  124.                 // Create the movie controller
  125.                    CreateNewMovieController(hwnd, movieStuff->theMovie, &movieStuff->theMC);
  126.                 movieStuff->movieOpened = TRUE;
  127.                 isMovieGood = TRUE;    
  128.                 p2cstr((char*)theFullPath);
  129.  
  130.                 SetWindowTitle(movieStuff->theHwnd, theFullPath);
  131.             } else
  132.                 theFullPath[0] = '\0'; 
  133.                 
  134.         } else
  135.             theFullPath[0] = '\0';
  136.     }
  137.  
  138.     return isMovieGood;
  139. }
  140.  
  141. OSErr SaveMovie(MovieStuff *movieStuff)
  142. {
  143.     OSErr            theErr = noErr;
  144.     if (strlen(movieStuff->filename) != 0) {
  145.         long            movieFlattenFlags = flattenAddMovieToDataFork;
  146.         FSSpec            sfFile;
  147.         OSType            creator = FOUR_CHAR_CODE('TVOD');
  148.         long            createMovieFlags = createMovieFileDeleteCurFile;
  149.         char            theFullPath[255];
  150.  
  151.         strcpy(theFullPath, movieStuff->filename);
  152.  
  153.         // Convert full path name to pstring
  154.         c2pstr((char *)theFullPath);    
  155.  
  156.         // Make a FSSpec with a pascal string filename
  157.         FSMakeFSSpec(0,0L,theFullPath, &sfFile);
  158.  
  159.         // Try to delete the original
  160.         DeleteMovieFile(&sfFile);        
  161.  
  162.         // FlattenMovie
  163.         FlattenMovie(    movieStuff->theMovie,
  164.                         movieFlattenFlags,
  165.                         &sfFile,
  166.                         creator,
  167.                         -1,
  168.                         createMovieFlags,
  169.                         nil,
  170.                         NULL );
  171.     
  172.         theErr = GetMoviesError();
  173.     }
  174.     return theErr;
  175. }
  176.  
  177. OSErr SaveAsMovie(MovieStuff *movieStuff)
  178. {
  179.     unsigned char    lpszPathName[256];
  180.     OPENFILENAME    ofn;
  181.     OSErr            theErr = noErr;
  182.  
  183.     memset(&ofn, 0, sizeof(OPENFILENAME));
  184.     lpszPathName[0] = '\0';
  185.  
  186.     ofn.lStructSize = sizeof(OPENFILENAME);
  187.     ofn.hwndOwner = GetActiveWindow();
  188.     ofn.lpstrFilter = "QuickTime Movies (*.mov) \0 *.mov\0";
  189.     ofn.lpstrFile = (char *)lpszPathName;
  190.     ofn.nMaxFile = sizeof(lpszPathName);
  191.     ofn.lpstrFileTitle = NULL;
  192.     ofn.nMaxFileTitle = (unsigned long)NULL;
  193.     ofn.lpstrInitialDir = NULL;
  194.     ofn.Flags = OFN_OVERWRITEPROMPT;
  195.  
  196.     // Put up a save file dialog
  197.     if (GetSaveFileName(&ofn)) {
  198.         long            movieFlattenFlags = flattenAddMovieToDataFork;
  199.         FSSpec            sfFile;
  200.         OSType            creator = FOUR_CHAR_CODE('TVOD');
  201.         long            createMovieFlags = createMovieFileDeleteCurFile;
  202.  
  203.         // Convert full path name to pstring
  204.         c2pstr((char *)lpszPathName);    
  205.  
  206.         // Make a FSSpec with a pascal string filename
  207.         FSMakeFSSpec(0,0L,lpszPathName, &sfFile);
  208.  
  209.         // Try to delete the original
  210.         DeleteMovieFile(&sfFile);        
  211.  
  212.         // FlattenMovie
  213.         FlattenMovie(    movieStuff->theMovie,
  214.                         movieFlattenFlags,
  215.                         &sfFile,
  216.                         creator,
  217.                         -1,
  218.                         createMovieFlags,
  219.                         nil,
  220.                         NULL );
  221.  
  222.         theErr = GetMoviesError();
  223.         p2cstr((char *)lpszPathName);
  224.         SetWindowTitle(movieStuff->theHwnd, lpszPathName);
  225.     }
  226.     return theErr;
  227. }
  228.  
  229. void CloseMovie(MovieStuff *movieStuff)
  230. {
  231.     if (movieStuff->movieOpened == TRUE ){
  232.         movieStuff->movieOpened = FALSE;
  233.      
  234.         if (movieStuff->theMC)
  235.             DisposeMovieController(movieStuff->theMC);
  236.  
  237.         if (movieStuff->theMovie)
  238.             DisposeMovie(movieStuff->theMovie);
  239.  
  240.         movieStuff->theMovie = NULL;
  241.         movieStuff->theMC = NULL;
  242.     }
  243. }
  244.  
  245.  
  246. void CreateNewMovieController(HWND hwnd, Movie theMovie, MovieController *theMC)
  247. {
  248.     Rect    bounds;
  249.     Rect    maxBounds;
  250.     long     controllerFlags;
  251.     Rect    theMovieRect;
  252.  
  253.     // 0,0 Movie coordinates
  254.     GetMovieBox(theMovie, &theMovieRect);
  255.     MacOffsetRect(&theMovieRect, -theMovieRect.left, -theMovieRect.top);
  256.  
  257.     // Attach a movie controller
  258.     *theMC = NewMovieController(theMovie, &theMovieRect, mcTopLeftMovie );
  259.  
  260.     // Get the controller rect 
  261.     MCGetControllerBoundsRect(*theMC, &bounds);
  262.  
  263.     // Enable editing
  264.     MCEnableEditing(*theMC,TRUE);
  265.  
  266.     // Tell the controller to attach a movie's CLUT to the window as appropriate.
  267.     MCDoAction(*theMC, mcActionGetFlags, &controllerFlags);
  268.     MCDoAction(*theMC, mcActionSetFlags, (void *)(controllerFlags | mcFlagsUseWindowPalette));
  269.  
  270.     // Allow the controller to accept keyboard events
  271.     MCDoAction(*theMC, mcActionSetKeysEnabled, (void *)TRUE);
  272.  
  273.     // Set the controller action filter
  274.     MCSetActionFilterWithRefCon(*theMC, MCFilter, (long)hwnd);
  275.  
  276.     // Set the grow box amound
  277.     GetMaxBounds(&maxBounds);
  278.     MCDoAction(*theMC, mcActionSetGrowBoxBounds, &maxBounds);
  279.  
  280.     // Size our window
  281.     SizeWindow((WindowPtr)GetNativeWindowPort(hwnd), bounds.right, bounds.bottom, FALSE);
  282. }
  283.  
  284.  
  285. Boolean MCFilter(MovieController mc, short action, void*params, long refCon)
  286. {
  287.     if(action == mcActionControllerSizeChanged) {
  288.         Rect        bounds;
  289.         WindowPtr    w;
  290.         MCGetControllerBoundsRect(mc, &bounds);
  291.  
  292.         w = GetNativeWindowPort((HWND)refCon);
  293.         SizeWindow((WindowPtr)w, bounds.right, bounds.bottom, TRUE);
  294.     }
  295.     return FALSE;
  296. }
  297.  
  298. void GetMaxBounds(Rect *maxRect)
  299. {
  300.     RECT deskRect;
  301.  
  302.     GetWindowRect(GetDesktopWindow(), &deskRect);
  303.  
  304.     OffsetRect(&deskRect, -deskRect.left, -deskRect.top);
  305.  
  306.     maxRect->top = (short)deskRect.top;
  307.     maxRect->bottom = (short)deskRect.bottom;
  308.     maxRect->left = (short)deskRect.left;
  309.     maxRect->right = (short)deskRect.right;
  310. }
  311.  
  312. ComponentResult EditCut(MovieController mc) 
  313. {
  314.     Movie                scrapMovie;
  315.     ComponentResult        theErr = invalidMovie;
  316.     
  317.     if (mc){
  318.         scrapMovie = MCCut(mc);
  319.         if ( scrapMovie ) {
  320.             theErr = PutMovieOnScrap(scrapMovie, 0L);
  321.             DisposeMovie(scrapMovie);
  322.         } 
  323.     }
  324.     return theErr;
  325. }
  326.  
  327. ComponentResult EditCopy(MovieController mc) 
  328. {
  329.     Movie                scrapMovie;
  330.     ComponentResult        theErr = invalidMovie;
  331.     
  332.     if (mc){
  333.         scrapMovie = MCCopy(mc);
  334.         if ( scrapMovie ) {
  335.             theErr = PutMovieOnScrap(scrapMovie, 0L);
  336.             DisposeMovie(scrapMovie);
  337.         }
  338.     }
  339.     return theErr;
  340. }
  341.  
  342. ComponentResult EditPaste(MovieController mc) 
  343. {
  344.     ComponentResult        theErr = invalidMovie;
  345.  
  346.     if (mc)
  347.         theErr = MCPaste(mc, nil);
  348.  
  349.     return theErr;
  350. }
  351.  
  352. ComponentResult EditClear(MovieController mc) 
  353. {
  354.     ComponentResult        theErr = invalidMovie;
  355.     
  356.     if (mc)
  357.         theErr = MCClear(mc);
  358.  
  359.     return theErr;
  360. }
  361.  
  362. ComponentResult EditUndo(MovieController mc) 
  363. {
  364.     ComponentResult        theErr = invalidMovie;
  365.  
  366.     if (mc)
  367.         theErr = MCUndo(mc);
  368.     
  369.     return theErr;
  370. }
  371.  
  372. ComponentResult EditSelectAll(Movie movie, MovieController mc) 
  373. {
  374.     TimeRecord             tr;
  375.     ComponentResult        theErr = noErr;
  376.     
  377.     if ( movie && mc ) {
  378.         tr.value.hi = 0;
  379.         tr.value.lo = 0;
  380.         tr.base = 0;
  381.         tr.scale = GetMovieTimeScale(movie);
  382.         MCDoAction(mc, mcActionSetSelectionBegin, &tr);
  383.         tr.value.lo = GetMovieDuration(movie);
  384.         MCDoAction(mc, mcActionSetSelectionDuration, &tr);
  385.     } else {
  386.         if ( movie == NULL )
  387.             theErr = invalidMovie;
  388.         else
  389.             theErr = -1;
  390.     }
  391.     return theErr;
  392. }
  393.  
  394. void SetWindowTitle(HWND hWnd, unsigned char *theFullPath) 
  395. {
  396.     /* set window title to name */
  397.     unsigned char    titleName[256];
  398.     titleName[0] = '\0';
  399.  
  400.     GetFileNameFromFullPath(theFullPath, (unsigned char *)&titleName);
  401.     SetWindowText(hWnd, (const char *)titleName);
  402. }
  403.  
  404. void GetFileNameFromFullPath(unsigned char *theFullPath, unsigned char *fileName) 
  405. {
  406.     /* pluck the filename from the fullpath, */
  407.     int        i = 0, j = -1, stringLen = 0;
  408.  
  409.     stringLen = strlen((char *)theFullPath);
  410.     if (stringLen > 0 ) {
  411.         while(i<stringLen){
  412.             if (theFullPath[i] == 0x5c || theFullPath[i] == '/' )
  413.                 j = i;
  414.             i++;
  415.         }
  416.         if ( j>-1)
  417.             strcpy((char *)fileName, (char *)&theFullPath[j+1]);
  418.         else
  419.             strcpy((char *)fileName, (char *)theFullPath);
  420.  
  421.     }
  422. }
  423.